(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数


	var params = {
		offset : 0 ,
		limit : 10 ,
		quizId : "" ,	 //竞猜id
		teamName : "" ,				//队伍名称
		beginDate : "" ,				//开始时间
		endDate : "" ,					//结束时间
		goldType : "" ,				//币种
		fromType : 1					//来源
	}
	//获取列表
	function getlist(curpage,pagenum){
	   $(".box-body").mLoading();

	   $.ajax({
			url: httpOrigin + '/quiz/contest/bet/order.json',
			type:'POST',
			async:true,
			data : params ,
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
                    dataset=data.t.result;
					dataset.forEach(function (item,index ) {
						item.formatTime = new Date( item.createTime).format("yyyy-MM-dd hh:mm:ss");
					},this )
					if(dataset && dataset.length > 0){
					   var html = template('datalist',{dataset:dataset});
					   $("#example1").html(html);
					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage >= totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					} else {
						var html = "<tr><td colspan='8'>暂无数据</td></tr>";
						$("#example1").html(html);

						totalPage = 1;
						$(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
						$(".curpage").val(curpage);
						if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
						if(curpage >= totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					}
				}
			} ,
		   	complete : function () {
				$(".box-body").mLoading("hide");
			}
	   });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();



		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
						params.offset = ( curpage - 1 ) * params.limit;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
						params.offset = ( curpage - 1 ) * params.limit;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
						params.offset = ( curpage - 1 ) * params.limit;
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)

			//类型
			$(".lottType").change(function () {
				if( $( this).val() == 0 ) {
					$(".gold").get(0).selectedIndex = 2;
					$(".gold").attr("disabled" , true );

					params.goldType = $(".gold").val();
				} else {
					$(".gold").attr("disabled" , false );
				}
				params.fromType = $(this).val();
			})
			//竞猜id
			$(".lott-id").change(function () {
				params.quizId = $(this).val();
			})
			//队伍名称
			$(".player").change(function () {
				params.teamName = $(this).val();
			})
			//币种
			$(".gold").change(function () {
				params.goldType = $(this).val();
			})
			//开始日期
			$("#datepicker").change(function () {
				params.beginDate = $(this).val();
			})
			//结束日期
			$("#datepicker2").change(function () {
				params.endDate = $(this).val();
			})
			//搜索按钮
			$(".search").click(function () {
				params.offset = 0;
				curpage = 1;
				getlist(curpage, totalPage);
			})
		},
		//渲染页面
		render:function(){
			var queryParam,items,hashKey = new Object(),key,val;
			if( location.search != "" ) {
				queryParam = location.search.substr( 1 , location.search.length );

				if( queryParam.indexOf("&") > -1 ) {
					items = queryParam.split("&");


					for( var i = 0; i < items.length; i ++ ) {

						key = items[i].split("=")[0];
						val = items[i].split("=")[1];
						hashKey[key] = val;

					}
				} else {
					key = items[i].split("=")[0];
					val = items[i].split("=")[1];
					hashKey[key] = val;
				}
				$(".lott-id").val( hashKey.id );
				params.quizId = hashKey.id;


				for( var i = 0, opts = $(".lottType option"); i < opts.length; i ++ ) {

					if( hashKey.type == $(opts[i]).val() ) {
						$(".lottType").get(0).selectedIndex = i;
					}
				}
			}


			getlist(curpage,pagenum)
		}
	};
	Page.init();
})();